class Registry:
    """注册器基类"""
    
    def __init__(self, name):
        self._name = name
        self._module_dict = dict()
        
    def _register_module(self, module_class, module_name=None, force=False):
        """注册模块"""
        if module_name is None:
            module_name = module_class.__name__
            
        if not force and module_name in self._module_dict:
            raise KeyError(f'{module_name} is already registered in {self._name}')
            
        self._module_dict[module_name] = module_class
        
    def register(self, name=None, force=False):
        """装饰器形式的注册方法"""
        def _register(cls):
            self._register_module(cls, name, force=force)
            return cls
        return _register
        
    def get(self, name):
        """获取注册的模块"""
        if name not in self._module_dict:
            raise KeyError(f'Module {name} is not registered in {self._name}')
        return self._module_dict[name]
        
    def __contains__(self, name):
        return name in self._module_dict
        
    def __iter__(self):
        return iter(self._module_dict.items())
        
    def keys(self):
        return self._module_dict.keys()